package com.xnyzc.lhy.statistics.service.impl.trajectory;

import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.statistics.component.es.JestService;
import com.xnyzc.lhy.statistics.entity.order.OaCOrderPersonal;
import com.xnyzc.lhy.statistics.entity.trajectory.OaDVehicleGpsHistory;
import com.xnyzc.lhy.statistics.entity.trajectory.OaSysTrajectory;
import com.xnyzc.lhy.statistics.service.trajectory.TrajectoryService;
import io.searchbox.client.JestResult;
import io.searchbox.core.Bulk;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 轨迹服务
 *
 * @author cuixiaojian
 * @date 2020-01-06
 */
@Slf4j
@Service
public class TrajectoryServiceImpl implements TrajectoryService {

    @Autowired
    private JestService restService;

    /**
     * 预估轨迹查询
     * @param orderNo 订单号
     * @return Rv
     */
    @Override
    public Rv getPredictedTrajectory(String orderNo) {

        if (CheckUtil.strIsEmpty(orderNo)) {
            throw PangException.create(EErrorCode.missingArg);
        }

        JestResult jestResult = restService.trajectoryMatchQuery(OaSysTrajectory.ES_ORDER_NO, orderNo, OaSysTrajectory.INDEX_NAME, OaSysTrajectory.TYPE);

        List<OaSysTrajectory> oaSysTrajectories = null;

        try {

            oaSysTrajectories = jestResult.getSourceAsObjectList(OaSysTrajectory.class);

        } catch (Exception e) {
            log.error("JestResult 类型转换 getSourceAsObjectList 为 OaSysTrajectory 类型出错：{}", e);
            throw PangException.create(EErrorCode.predictedTrajectory);
        }

        return Rv.wrap(oaSysTrajectories);

    }

    /**
     * 订单轨迹查询
     * @param orderNo 订单号
     * @return Rv
     */
    @Override
    public Rv getOrderTrajectory(String orderNo) {

        if (CheckUtil.strIsEmpty(orderNo)) {
            throw PangException.create(EErrorCode.missingArg);
        }

        JestResult jestResult = restService.trajectoryMatchQuery(OaDVehicleGpsHistory.ES_ORDER_NO, orderNo, OaDVehicleGpsHistory.INDEX_NAME, OaDVehicleGpsHistory.TYPE);

        List<OaDVehicleGpsHistory> oaDVehicleGpsHistories = null;

        try {

            oaDVehicleGpsHistories = jestResult.getSourceAsObjectList(OaDVehicleGpsHistory.class);

        } catch (Exception e) {
            log.error("JestResult 类型转换 getSourceAsObjectList 为 OaDVehicleGpsHistory 类型出错：{}", e);
            throw PangException.create(EErrorCode.predictedTrajectory);
        }

        return Rv.wrap(oaDVehicleGpsHistories);

    }

    /**
     * 预估轨迹保存
     * @param oaSysTrajectoryList 预估轨迹集合
     * @return Rv
     */
    @Override
    public Rv savePredictedTrajectory(List<OaSysTrajectory> oaSysTrajectoryList) {

        JestResult jestResult=null;

        if (CheckUtil.collectionIsEmpty(oaSysTrajectoryList)) {
            throw PangException.create(EErrorCode.missingArg);
        }

        Bulk.Builder bulk = new Bulk.Builder();

        for (OaSysTrajectory oaSysTrajectory : oaSysTrajectoryList) {
            Index index = new Index.Builder(oaSysTrajectory).index(OaSysTrajectory.INDEX_NAME).type(OaSysTrajectory.TYPE).id(oaSysTrajectory.getTrajectoryId().toString()).build();
            bulk.addAction(index);
        }

        jestResult = restService.jestExecute(bulk.build());

        return Rv.wrap(jestResult.isSucceeded());

    }

    /**
     * 订单轨迹保存
     * @param oaDVehicleGpsHistoryList 订单轨迹集合
     * @return Rv
     */
    @Override
    public Rv saveOrderTrajectory(List<OaDVehicleGpsHistory> oaDVehicleGpsHistoryList) {

        if (CheckUtil.collectionIsEmpty(oaDVehicleGpsHistoryList)) {
            throw PangException.create(EErrorCode.missingArg);
        }

        JestResult jestResult=null;

        Bulk.Builder bulk = new Bulk.Builder();

        for (OaDVehicleGpsHistory oaDVehicleGpsHistory : oaDVehicleGpsHistoryList) {
            Index index = new Index.Builder(oaDVehicleGpsHistory).index(OaDVehicleGpsHistory.INDEX_NAME).type(OaDVehicleGpsHistory.TYPE).id(oaDVehicleGpsHistory.getHistoryId().toString()).build();
            bulk.addAction(index);
        }

        jestResult = restService.jestExecute(bulk.build());

        return Rv.wrap(jestResult.isSucceeded());

    }

}
